package com.xonro.common.histrans.sfeg.biz.cm;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.emm.util.DateUtil;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.base.consts.SfegConst;
import com.xonro.common.histrans.sfeg.biz.CommTransBiz;
import com.xonro.common.histrans.sfeg.biz.common.SfegProcessTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class ContractAcountBiz extends SfegProcessTransBiz {
    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();

        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_XR_CM_REPORT_OVER");//合同结算
        logModel.setOuterTable("tb_ContractFinalAccounts");//合同结算
        logModel.setOuterPK(mainData.getString("cfaCode"));
        logModel.setOuterExt1(mainData.getString("ctCode"));
        logModel.setOuterExt2(mainData.getString("planContent"));//项目类别
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        mainBO.set("APPLY_DATE", DateUtil.parse( mainData.getString("applyDate"), "yyyy-MM-dd"));
        mainBO.set("PROJECT_NO",mainData.getString("xmbh"));//项目编号
        mainBO.set("APPLY_NO", mainData.getString("cfaCode"));//申请单号
        mainBO.set("CONTRACT_NO", mainData.getString("ctCode"));//合同编号
        mainBO.set("CENSOR_AMOUT", mainData.getString("planTotal"));//送审价格
        mainBO.set("DEDUCT_AMOUNT", mainData.getString("deductMoney"));//应扣款，扣款金额
        mainBO.set("SELLTE_DATE", DateUtil.parse( mainData.getString("planDate"), "yyyy-MM-dd"));//决算时间
        mainBO.set("APPLY_DATE", DateUtil.parse( mainData.getString("submitDate"), "yyyy-MM-dd"));//提交日期
        mainBO.set("REMARK", mainData.getString("planContent"));//决算内容，结算内容
        mainBO.set("PROJECT_DESC", mainData.getString("finishCircs"));//工程量完成情况
        mainBO.set("APPLY_DEPT_ID", mainData.getString("planDept"));//结算部门
        mainBO.set("USER_ID", CommTransBiz.getInstance().getUserId( mainData.getString("planUser")));//结算人
        mainBO.set("SETTLE_AMOUNT", mainData.getString("realTotal"));//报结价格

        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);


        //添加附件
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        //OA表订单编号
        fileTransData.setFkCode(mainData.getString("cfaCode"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        mainBOCreateData.setBoName("BO_XR_CM_REPORT_OVER");
        fileTransData.setBoName("BO_XR_CM_REPORT_OVER");
        fileTransData.setFieldName("ATTACHMENT");
    }

    @Override
    protected String getProcessDefId() {
        return "obj_1fae73536a2f4f5f989ffd090ed544ba";
    }

    @Override
    protected String getActivityDefId() {
        return "obj_12e7d4107fc441998ad4daa6e6a8d732";
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        String cfaCode = ctx.getMainData().getString("cfaCode");//决策编号
        String planContent = ctx.getMainData().getString("planContent");//决策内容
        return planContent+cfaCode;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        return ctx.getMainData().getString("cfaCode");//决策编号
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return "tb_ContractFinalAccounts";
    }

    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        return historyTransContext.getMainData().getString("cfaCode");//决策编号
    }

    @Override
    protected List<RowMap> getMainDatas() {
        return SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMaps("SELECT * FROM tb_ContractFinalAccounts WHERE fillDept like '1040%'");
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }
}
